{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "KEY = 'c25fe3dd21454182b051ac79e291af1d' \n",
    "BASE_URL = 'https://api-cq.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),  \n",
    "}\n",
    "\n",
    "img_url = 'http://dingyue.nosdn.127.net/EqhQ0fEL0ML3uLL07ISGgzsr=QajEHp=zU9EoaO7Qaleh1534991913857.jpg'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': 'b667c478-1d0b-4534-a958-078f1891f10b',\n",
       "  'faceRectangle': {'top': 268, 'left': 141, 'width': 321, 'height': 321},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 18.0,\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.033,\n",
       "    'neutral': 0.967,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0}}}]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)\n",
    "\n",
    "r.status_code\n",
    "r.json()\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceAttributes.emotion.anger</th>\n",
       "      <th>faceAttributes.emotion.contempt</th>\n",
       "      <th>faceAttributes.emotion.disgust</th>\n",
       "      <th>faceAttributes.emotion.fear</th>\n",
       "      <th>faceAttributes.emotion.happiness</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.emotion.sadness</th>\n",
       "      <th>faceAttributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b667c478-1d0b-4534-a958-078f1891f10b</td>\n",
       "      <td>268</td>\n",
       "      <td>141</td>\n",
       "      <td>321</td>\n",
       "      <td>321</td>\n",
       "      <td>male</td>\n",
       "      <td>18.0</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.033</td>\n",
       "      <td>0.967</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId  faceRectangle.top  \\\n",
       "0  b667c478-1d0b-4534-a958-078f1891f10b                268   \n",
       "\n",
       "   faceRectangle.left  faceRectangle.width  faceRectangle.height  \\\n",
       "0                 141                  321                   321   \n",
       "\n",
       "  faceAttributes.gender  faceAttributes.age faceAttributes.glasses  \\\n",
       "0                  male                18.0              NoGlasses   \n",
       "\n",
       "   faceAttributes.emotion.anger  faceAttributes.emotion.contempt  \\\n",
       "0                           0.0                              0.0   \n",
       "\n",
       "   faceAttributes.emotion.disgust  faceAttributes.emotion.fear  \\\n",
       "0                             0.0                          0.0   \n",
       "\n",
       "   faceAttributes.emotion.happiness  faceAttributes.emotion.neutral  \\\n",
       "0                             0.033                           0.967   \n",
       "\n",
       "   faceAttributes.emotion.sadness  faceAttributes.emotion.surprise  \n",
       "0                             0.0                              0.0  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df_ax = pd.json_normalize(results)\n",
    "df_ax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>年龄</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceAttributes.emotion.anger</th>\n",
       "      <th>faceAttributes.emotion.contempt</th>\n",
       "      <th>faceAttributes.emotion.disgust</th>\n",
       "      <th>faceAttributes.emotion.fear</th>\n",
       "      <th>faceAttributes.emotion.happiness</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.emotion.sadness</th>\n",
       "      <th>faceAttributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b667c478-1d0b-4534-a958-078f1891f10b</td>\n",
       "      <td>268</td>\n",
       "      <td>141</td>\n",
       "      <td>321</td>\n",
       "      <td>321</td>\n",
       "      <td>male</td>\n",
       "      <td>18.0</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.033</td>\n",
       "      <td>0.967</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId  faceRectangle.top  \\\n",
       "0  b667c478-1d0b-4534-a958-078f1891f10b                268   \n",
       "\n",
       "   faceRectangle.left  faceRectangle.width  faceRectangle.height  \\\n",
       "0                 141                  321                   321   \n",
       "\n",
       "  faceAttributes.gender    年龄 faceAttributes.glasses  \\\n",
       "0                  male  18.0              NoGlasses   \n",
       "\n",
       "   faceAttributes.emotion.anger  faceAttributes.emotion.contempt  \\\n",
       "0                           0.0                              0.0   \n",
       "\n",
       "   faceAttributes.emotion.disgust  faceAttributes.emotion.fear  \\\n",
       "0                             0.0                          0.0   \n",
       "\n",
       "   faceAttributes.emotion.happiness  faceAttributes.emotion.neutral  \\\n",
       "0                             0.033                           0.967   \n",
       "\n",
       "   faceAttributes.emotion.sadness  faceAttributes.emotion.surprise  \n",
       "0                             0.0                              0.0  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_ax = df_ax.rename(columns = {\"faceAttributes.age\":\"年龄\"\n",
    "                                \n",
    "                                \n",
    "                              })\n",
    "df_ax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
